﻿using SCM.Common;
using SCM.Common.Extensions;
using SCM.Model.DBEntity;
using SCM.Servie.IBusiness;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace SCM.Servie.Business
{
    /// <summary>
    /// MCD 成分服务
    /// </summary>
    public class MaterialMCDIngredientService : IMaterialMCDIngredientService
    {
        private readonly DbContext dal = new DbContext();

        public string Add(T_Material_MCDIngredient model)
        {
            return dal.Insertable(model).ExecuteReturnEntity().ID;
        }

        public bool Delete(List<string> ids)
        {
            return dal.Deleteable<T_Material_MCDIngredient>(p => ids.Contains(p.ID)).ExecuteCommand() > 0;
        }

        public T_Material_MCDIngredient GetById(string id)
        {
            return dal.Get<T_Material_MCDIngredient>(id);
        }

        public List<T_Material_MCDIngredient> GetList(string mcdHomogeneousID)
        {
            Expression<Func<T_Material_MCDIngredient, bool>> whereExp = (a) => true;
            if (!string.IsNullOrWhiteSpace(mcdHomogeneousID))
            {
                whereExp = whereExp.Compose((a) => a.MCDHomogeneousID == mcdHomogeneousID, Expression.AndAlso);
            }
            return dal.Queryable<T_Material_MCDIngredient>().Where(whereExp).ToList();
        }

        public PageData<T_Material_MCDIngredient> Query(string mcdHomogeneousID, PageInfo page)
        {
            int count = 0;
            Expression<Func<T_Material_MCDIngredient, bool>> whereExp = (a) => true;
            if (!string.IsNullOrWhiteSpace(mcdHomogeneousID))
            {
                whereExp = whereExp.Compose((a) => a.MCDHomogeneousID == mcdHomogeneousID, Expression.AndAlso);
            }
            List<T_Material_MCDIngredient> list = dal.Queryable<T_Material_MCDIngredient>().Where(whereExp).ToPageList(page.PageIndex, page.PageSize, ref count);
            page.DataCount = count;
            return new PageData<T_Material_MCDIngredient> { Data = list, Page = page };
        }

        public bool Update(T_Material_MCDIngredient model)
        {
            return dal.Modify(model);
        }
    }
}
